package cn.jxufe.imp;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import cn.jxufe.bean.EasyUIData;
import cn.jxufe.bean.EasyUIDataPageRequest;
import cn.jxufe.bean.Message;
import cn.jxufe.dao.CropsGrowDAO;
import cn.jxufe.entity.CropsGrow;
import cn.jxufe.entity.Seed;
import cn.jxufe.service.CropsGrowService;

@Service
public class CropsGrowImp implements CropsGrowService{
	@Autowired
	CropsGrowDAO cropsGrowDAO;

	@Override
	public Message save(CropsGrow cropsGrow) {
		String result = "OK";
		Message message = new Message();
		try {
			cropsGrowDAO.save(cropsGrow);
			message.setCode(0);
			message.setMsg("数据保存成功");
		}catch(Exception e) {
			e.printStackTrace();
			message.setCode(1);
			message.setMsg("数据保存错误");
		}
		return message;
	}

	@Override
	public Message delete(CropsGrow cropsGrow) {
		String result = "OK";
		Message message = new Message();
		try {
			cropsGrowDAO.delete(cropsGrow);
			message.setCode(0);
			message.setMsg("数据删除成功");
		}catch (Exception e) {
			e.printStackTrace();
			message.setCode(1);
			message.setMsg("数据删除出错");
		}
		return message;
	}

	@Override
	public List<CropsGrow> list(int seedId, EasyUIDataPageRequest easyUIDataPageRequest) {
		EasyUIData<CropsGrow> result = new EasyUIData<CropsGrow>();
		List<Sort.Order> orders = new ArrayList<Sort.Order>();
		if(easyUIDataPageRequest.getOrder().equals("asc")) {
            orders.add(new Sort.Order(Direction.ASC, easyUIDataPageRequest.getSort()));
        }else {
            orders.add(new Sort.Order(Direction.DESC, easyUIDataPageRequest.getSort()));
        }
		return cropsGrowDAO.findBySeedId(seedId, new Sort(orders));
	}

	// 获取植物第五阶段的信息
	@Override
	public List<CropsGrow> getCropsStage5(String stageImage) {
		
		return cropsGrowDAO.findByStageImage(stageImage);
	}

}
